import windowConst from "@/utils/const";
import axios from 'axios'
import {message} from './message'
import Cookie from './cookie'
axios.defaults.withCredentials = true
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

function ajax(url, type, params, config) {
    return new Promise((resolve, reject) => {
        axios({
            method: type,
            // url: config ? config[config.host] + url : url,
            // url: "http://localaitalk.zj.chinamobile.com:8080"+url,
            // url: "http://testaitalk.zj.chinamobile.com:8080/aitalk"+url,
            url: windowConst.VUE_APP_API_HOST + url,
            timeout: 10000,
            headers: {
                'aitalk_token': Cookie.getCookie("aitalk_token") || ''
            },
            responseType: params.responseType||'',
            params: type === 'get' ? params : null,
            data: params
        })
            .then(result => {
                let msg;
                if (result) {
                    //2021-02-02 shenyi add 判断是否为二进制数据
                    const cType = result.headers["content-type"];
                    if(cType && cType.indexOf("application/x-msdownload") !== -1){
                        resolve(result.data);
                        return
                    }else{
                        const data = result.data;
                        const code = data.result;
                        msg = data.message;
                        if (code === "1") {
                            resolve(data)
                            return
                        } else {
                            reject(data);
                            if(msg==="token认证失败！"){//2020-08-20 shenyi add 认证失败转到login
                             const redirectAddressUrl = window.location.href
                              //当认证失败的时候，需要保存当前页面地址，跳转到登录页面后，登录成功后，跳转回当前页面
                             sessionStorage.setItem("redirectAddressUrl",redirectAddressUrl)
                             location.replace(windowConst.VUE_APP_API_HOST + "/index/talk.htm");//2021-03-01 修改为从配置文件中获取主机地址
                             return;
                            }
                        }
                    }
                    
                }
                msg = msg || "系统打盹了，休息一会。"
                message.error({
                    message: msg,
                    type: 'error',
                    duration: 2 * 1000
                })
            })
            .catch(error => {
                message.error({
                    message: "系统打盹了，休息一会。",
                    duration: 2 * 1000
                })
                resolve(error)
            })
    })
}

const config = {
    get(url, params, config) {
        return new Promise((resolve, reject) => {
            ajax(url, 'get', params, config).then(
                data => {
                    resolve(data)
                },
                error => {
                    reject(error)
                }
            )
        })
    },
    post(url, params, config) {
        return new Promise((resolve, reject) => {
            ajax(url, 'post', params, config).then(
                data => {
                    resolve(data)
                },
                error => {
                    reject(error)
                }
            )
        })
    }
}

export default config
